{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1.2, 1.5, 1.8], [1.3, 1.4, 1.9], [1.1, 1.6, 1.7]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.array([\n",
    "    [1.2, 1.5, 1.8], \n",
    "    [1.3, 1.4, 1.9], \n",
    "    [1.1, 1.6, 1.7]\n",
    "])\n",
    "y = np.array([5, 10, 9]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一\n",
    "# 使用循环的方式计算每天的采购金额得到结果为【37.2,37.6,36.8】，分表示7/28,7/29,7/30这三天的采购量\n",
    "Z = np.array([0.0,0.0,0.0])\n",
    "for i in range(3):\n",
    "    for j in range(3):\n",
    "        Z[i] = Z[i] + X[i][j] * y[j]\n",
    "Z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方式二 \n",
    "# 直接调用np.dot直接得出三天的采购量\n",
    "Z2 = np.dot(X,y)\n",
    "Z2    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18.3 µs ± 738 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "# 测试两种方法的使用性能\n",
    "# 第一种\n",
    "Z = np.array([0.0,0.0,0.0])\n",
    "for i in range(3):\n",
    "    for j in range(3):\n",
    "        Z[i] = Z[i] + X[i][j] * y[j]\n",
    "Z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.27 µs ± 163 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "# 第二种\n",
    "Z2 = np.dot(X,y)\n",
    "Z2   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
